#include "ball.h"
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QWidget>
#include <QDebug>
#include <QGraphicsScene>
#include <QRectF>

Ball::Ball(qreal x, qreal y, qreal w, qreal h, std::vector<std::vector<QGraphicsEllipseItem*> >* cells,
           std::vector<std::vector<bool> >* isField)
    :QGraphicsEllipseItem(x, y, w, h), cells(cells), posBefore(this->pos()), isField(isField)
{
    posBefore.setX(x);
    posBefore.setY(y);
    setBrush(Qt::gray);
    setFlag(QGraphicsEllipseItem::ItemIsMovable, true);
    setFlag(QGraphicsEllipseItem::ItemIsFocusable, true);
    setFlag(QGraphicsEllipseItem::ItemSendsScenePositionChanges, true);
}

void Ball::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    static QImage image = QImage(":/pictures/ball.png");
    painter->drawImage(option->rect, image);
}

void Ball::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    QPointF newPos;
    newPos.setX(x());
    newPos.setY(y());
    int width = rect().width() + 2;
    if (abs(newPos.x()) > width * 3 / 2 && abs(newPos.x()) < width * 5 / 2 &&
            abs(newPos.y()) < width / 2)
    {
        int d = newPos.x() < 0 ? -2 : 2;
        int b = newPos.x() < 0 ? -1 : 1;
        int i = posBefore.x() / width;
        int j = posBefore.y() / width;
        if (i + d >= 0 && i + d < cells->size() && (*isField)[i + d][j] && !(*cells)[i + d][j] && (*cells)[i + b][j]) {
                QGraphicsEllipseItem* el = (*cells)[i][j];
                (*cells)[i + d][j] = el;
                scene()->removeItem((*cells)[i + b][j]);
                (*cells)[i + b][j] = 0;
                (*cells)[i][j] = 0;
                newPos.setX(d * width - x());
                newPos.setY(0 - y());
                posBefore.setX(posBefore.x() + d * width);
                setRect((i + d) * width + 1, j * width + 1, width - 2, width - 2);
                setPos(0, 0);
                moveBy(0, 0);
                return;
        }
    }
    if (abs(newPos.y()) > width * 3 / 2 && abs(newPos.y()) < width * 5 / 2 &&
            abs(newPos.x()) < width / 2)
    {
        int d = newPos.y() < 0 ? -2 : 2;
        int b = newPos.y() < 0 ? -1 : 1;
        int i = posBefore.x() / width;
        int j = posBefore.y() / width;
        if (j + d >= 0 && j + d < cells->size() && (*isField)[i][j + d] && !(*cells)[i][j + d] && (*cells)[i][j + b]) {
                QGraphicsEllipseItem* el = (*cells)[i][j];
                (*cells)[i][j + d] = el;
                scene()->removeItem((*cells)[i][j + b]);
                (*cells)[i][j + b] = 0;
                (*cells)[i][j] = 0;
                newPos.setX(0 - x());
                newPos.setY(d * width - y());
                posBefore.setY(posBefore.y() + d * width);
                setRect(i * width + 1, (j + d) * width + 1, width - 2, width - 2);
                setPos(0, 0);
                moveBy(0, 0);
                return;
        }
    }
      newPos.setX(-newPos.x());
      newPos.setY(-newPos.y());
      moveBy(newPos.x(), newPos.y());
}
